/**
 * 文件工具类
 * Created by xsx on 2018/1/9.
 */
var FileUtil = {};

// 文件上传
FileUtil.uploadFile = (function () {
    // 默认选项
    var defaultOption = {
        fileSavePath : "",
        projectDocType : "",
        fileFieldName : "file",
        file_isZoom : 0,
        file_size : "",
        success_fn : function (data) {

        }
    };

    // 上传监听
    function listenProgress(obj,panel,identify,statusName,fileID){
        //进度条生成
        var uploadProBg=$("<div class='ufProgBg'></div>");
        var uploadProgr=$("<div class='progress'></div>").appendTo(uploadProBg);
        $.wdfProgress.progress("正在上传文件0%...",panel);
        panel.find(".wdfProgress_Prog").append(uploadProBg);
        setTimeout(listener,100);
        function listener(){
            $.ajax({
                url:basePath+"file/wdfUpload.wdf?statusName="+statusName,
                //processData: false,
                //data: xmlDocument,
                dataType:"json",
                type: "GET",
                contentType:"application/x-www-form-urlencoded",
                beforeSend:null,
                success:function(data){
                    if(data.status&&data.status==1){
                        //上传进度
                        var percent = parseInt(data.data.percent);
                        uploadProgr.css("width",percent+"%");
                        $.wdfProgress.showMsg("正在上传文件"+percent+"%",panel);
                        if(percent == 100){
                            defaultOption.success_fn(data);
                            identify.success=1;
                        }else{
                            setTimeout(listener,100);
                        }

                    }else if(data.status==0){
                        identify.success=2;
                        $.wdfProgress.showMsg(data.msg,panel,true);

                    }

                },
                error:function (XMLHttpRequest, textStatus, errorThrown) {
                    identify.success=2;
                    $.wdfProgress.showMsg("网络错误或系统异常！",panel,true);
                }
            });
        }
    }

    // 参数检查
    function check(option) {
        if (typeof option == "undefined"){
            throw new Error("传入的配置参数不能为空");
        }
        if (typeof option.fileSavePath != "undefined"){
            defaultOption.fileSavePath = option.fileSavePath;
        }else if (typeof option.projectDocType != "undefined"){
            defaultOption.projectDocType = option.projectDocType;
        }else{
            throw new Error("fileSavePath与projectDocType选项必须有一项");
        }
        if (typeof option.fileFieldName != "undefined"){
            defaultOption.fileFieldName = option.fileFieldName;
        }
        if (typeof option.file_isZoom != "undefined"&&typeof option.file_isZoom == 1){
            defaultOption.file_isZoom = option.file_isZoom;
            if (typeof option.file_size != "undefined"&&/\d+x\d+/.test(option.file_size)){
                defaultOption.file_size = option.file_size;
            }else{
                throw new Error("file_size选项错误或不能为空,eg: file_size:'32x32'");
            }
        }
        if (typeof option.success_fn != "undefined"){
            if (typeof option.success_fn == "function"){
                defaultOption.success_fn = option.success_fn;
            }else{
                throw new Error("success_fn选项只能为函数");
            }
        }
    }

    // 返回通用上传方法
    return function (option) {
        check(option);

        var identify = {"success":0};
        var statusName = new String(new UUID()).replace(/\-+/g,"");
        var fileID = $("#file").val().substr($("#file").val().lastIndexOf('\\')+1);
        var panel = $("body");
        var uploadForm = $("#uploadFileForm");
        uploadForm.attr("action","");
        uploadForm.attr("action",basePath+"file/wdfUpload.wdf?statusName="+statusName);
        var action = uploadForm.attr("action");
        uploadForm.attr(
            "action",
            action+
            "&fileSavePath="+defaultOption.fileSavePath+
            "&projectDocType="+defaultOption.projectDocType+
            "&fileID="+fileID+
            "&"+defaultOption.fileFieldName+"_isZoom="+defaultOption.file_isZoom+
            "&"+defaultOption.fileFieldName+"_size="+defaultOption.file_size
        );
        var options  = {
            type:'post',
            success:function(data){

            }
        };
        uploadForm.ajaxSubmit(options);
        setTimeout(function(){
            listenProgress(this,panel,identify,statusName,fileID);
            },
            100
        );
    }
})();

// 格式化文件大小
FileUtil.formatSize = (function () {
   return function (value) {
       if(null==value||value==''){
           return "0 Bytes";
       }
       var unitArr = new Array("Bytes","KB","MB","GB","TB","PB","EB","ZB","YB");
       var index;
       var srcsize = parseFloat(value);
       index=Math.floor(Math.log(srcsize)/Math.log(1024));
       var size =srcsize/Math.pow(1024,index);
       size=size.toFixed(2);//保留的小数位数
       return size+unitArr[index];
   }
}());
